package com.gpj.learn.rocketmq.filter;

import com.gpj.learn.rocketmq.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;

/**
 * @className FilterProducer
 * @description
 *   TODO
 * @author GPJ
 * @date 2021/4/26 22:36
 * @version 1.0
 **/
@Slf4j
public class FilterProducer {
	public static void main(String[] args) {
		DefaultMQProducer producer = new DefaultMQProducer("FilterMessageProducerGroup");
		producer.setNamesrvAddr(CommonConstants.ROCKETMQ_NAMESERVER_ADDR);

		try {
			producer.start();

			String[] tags = new String[]{"TagA", "TagB", "TagC"};
			for (int i = 0; i < 9; i++) {
				Message message = new Message(CommonConstants.DEFAULT_TOPIC_NAME,
						tags[i % tags.length], "key-" + 1,
						"this is a filter message".getBytes(StandardCharsets.UTF_8));
				message.putUserProperty("mark", String.valueOf(i));

				SendResult result = producer.send(message);
				log.info("消息已发出:{}", result);
			}
		} catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
			e.printStackTrace();
		}finally {
			producer.shutdown();
		}
	}
}
